package sseGameApps.controller.mdkbr.main;

import java.util.HashMap;
import java.util.Map;

import org.slim3.controller.Navigation;

import sseGameApps.controller.JsonController;
import sseGameApps.model.mdkbr.MdKbrUser;
import sseGameApps.model.mdkbr.MdUseKanji;
import sseGameApps.service.mdkbr.MdKanjiService;
import sseGameApps.service.mdkbr.MdKbrUserService;
import sseGameApps.service.mdkbr.MdStrategyService;

public class MdFieldStartController extends JsonController {

    private MdKanjiService serviceMdKanji = new MdKanjiService();
    private MdKbrUserService serviceMdKbrUser = new MdKbrUserService();
    private MdStrategyService serviceMdStrategy = new MdStrategyService();

    @Override
    public Navigation run() throws Exception {
        Integer playerBarPoint = (int) (Math.round(Math.random() * 9999 + 0.5));
        Integer playerAttackPoint =
            (int) (Math.round(Math.random() * 200 + 0.5));

        Map<String, Integer> typeSpecialPointP = new HashMap<String, Integer>();
        MdUseKanji mdUseKanjiP =
            serviceMdKanji.getMdUseKanji(asString("userId"));
        MdKbrUser mdKbrUserP =
            serviceMdKbrUser.getMdKbrUser(asString("userId"));

        Map<String, String> data = new HashMap<String, String>();

        data.put("idCanvas", asString("idCanvas"));
        data.put("userId", asString("userId"));

        if (mdKbrUserP == null) {
            data.put("playerName", asString("userId"));
            data.put("playerCG", "m_001R.png");
            data.put("playerSchool", "001");
            data.put("playerSex", "1");
            data.put("playerType", "KI");
        } else {
            data.put("playerName", mdKbrUserP.getPlayerName());
            data.put("playerCG", mdKbrUserP.getPlayerCGType());
            data.put("playerSchool", mdKbrUserP.getPlayerSchool());
            data.put("playerSex", mdKbrUserP.getPlayerSex());
            data.put("playerType", mdKbrUserP.getPlayerType());
            data.put(
                "userLevel",
                serviceMdKbrUser
                    .checkUserLevel(mdKbrUserP.getUserExp())
                    .toString());
            data.put("userExp", mdKbrUserP.getUserExp().toString());
            data.put(
                "userNextLvExp",
                serviceMdKbrUser
                    .checkUserNextLevelExp(mdKbrUserP.getUserExp())
                    .toString());
        }

        if (mdUseKanjiP == null) {
            data.put("playerBarPoint", playerBarPoint.toString());
            data.put("playerBarPointBefore", playerBarPoint.toString());
            data.put("playerAttackPoint", playerAttackPoint.toString());
            data.put("playerKanji01", "壱");
            data.put("playerKanji02", "弐");
            data.put("playerKanji03", "参");
            data.put("playerTypeSp1", "0");
            data.put("playerTypeSp2", "0");
            data.put("playerTypeSp3", "0");
            data.put("playerTypeSp4", "0");
            data.put("playerTypeSp5", "0");
        } else {
            typeSpecialPointP = serviceMdKanji.getTypeSpecialPoint(mdUseKanjiP);
            data.put("playerBarPoint", mdUseKanjiP.getUserBp().toString());
            data
                .put("playerBarPointBefore", mdUseKanjiP.getUserBp().toString());
            data.put("playerAttackPoint", mdUseKanjiP.getUserAp().toString());
            data.put("playerKanji01", mdUseKanjiP.getUsingKanji01());
            data.put("playerKanji02", mdUseKanjiP.getUsingKanji02());
            data.put("playerKanji03", mdUseKanjiP.getUsingKanji03());
            data.put("playerTypeSp1", typeSpecialPointP
                .get("typeSpecialPoint1")
                .toString());
            data.put("playerTypeSp2", typeSpecialPointP
                .get("typeSpecialPoint2")
                .toString());
            data.put("playerTypeSp3", typeSpecialPointP
                .get("typeSpecialPoint3")
                .toString());
            data.put("playerTypeSp4", typeSpecialPointP
                .get("typeSpecialPoint4")
                .toString());
            data.put("playerTypeSp5", typeSpecialPointP
                .get("typeSpecialPoint5")
                .toString());
        }

        Integer enemyBarPoint = 0;
        Integer enemyAttackPoint = 0;

        if (asString("eneLv") == null || "".equals(asString("eneLv"))) {
            enemyBarPoint = (int) (Math.round(Math.random() * 9999 + 0.5));
            enemyAttackPoint =
                (int) (Math.round(Math.random() * 200 + 0.5)) + 200;
        } else {
            switch (asInteger("eneLv")) {
            case 1:
                enemyBarPoint =
                    100 + (int) (Math.round(Math.random() * 20 + 0.5));
                enemyAttackPoint =
                    30 + (int) (Math.round(Math.random() * 10 + 0.5));
                break;
            case 2:
                enemyBarPoint =
                    260 + (int) (Math.round(Math.random() * 50 + 0.5));
                enemyAttackPoint =
                    80 + (int) (Math.round(Math.random() * 20 + 0.5));
                break;
            case 3:
                enemyBarPoint =
                    600 + (int) (Math.round(Math.random() * 50 + 0.5));
                enemyAttackPoint =
                    200 + (int) (Math.round(Math.random() * 20 + 0.5));
                break;
            case 4:
                enemyBarPoint =
                    900 + (int) (Math.round(Math.random() * 250 + 0.5));
                enemyAttackPoint =
                    300 + (int) (Math.round(Math.random() * 50 + 0.5));
                break;
            case 5:
                enemyBarPoint =
                    1500 + (int) (Math.round(Math.random() * 50 + 0.5));
                enemyAttackPoint =
                    550 + (int) (Math.round(Math.random() * 20 + 0.5));
                break;
            case 6:
                enemyBarPoint =
                    2500 + (int) (Math.round(Math.random() * 100 + 0.5));
                enemyAttackPoint =
                    850 + (int) (Math.round(Math.random() * 50 + 0.5));
                break;
            default:
                enemyBarPoint =
                    3000 + (int) (Math.round(Math.random() * 3000 + 0.5));
                enemyAttackPoint =
                    1500 + (int) (Math.round(Math.random() * 1000 + 0.5));
                break;
            }
        }

        Map<String, Integer> typeSpecialPointE = new HashMap<String, Integer>();

        MdUseKanji mdUseKanjiE = null;
        MdKbrUser mdKbrUserE = null;

        if (asString("eneId") == null || "".equals(asString("eneId"))) {
            Integer userCount = serviceMdStrategy.getMdUseKanjiCount();
            mdUseKanjiE =
                serviceMdStrategy.getRundomMdUseKanji(
                    asString("userId"),
                    (int) (Math.round(Math.random() * userCount + 0.5)));
            if (mdUseKanjiE != null) {
                mdKbrUserE =
                    serviceMdKbrUser.getMdKbrUser(mdUseKanjiE.getUserId());
            }
        } else {
            mdUseKanjiE = serviceMdKanji.getMdUseKanji(asString("eneId"));
            mdKbrUserE = serviceMdKbrUser.getMdKbrUser(asString("eneId"));
        }

        if ("3".equals(asString("firstLogin"))) {
            data.put("eneId", "教官");
            data.put("enemyName", "教官");
            data.put("enemyBarPoint", "50");
            data.put("enemyBarPointBefore", "50");
            data.put("enemyAttackPoint", "30");
            data.put("enemyType", "KI");
            data.put("enemyKanji01", "壱");
            data.put("enemyKanji02", "弐");
            data.put("enemyKanji03", "参");
            data.put("enemyTypeSp1", "0");
            data.put("enemyTypeSp2", "0");
            data.put("enemyTypeSp3", "0");
            data.put("enemyTypeSp4", "0");
            data.put("enemyTypeSp5", "0");
            data.put("enemyCG", "m_000L.png");
        } else {

            if (mdKbrUserE == null) {
                data.put("enemyName", "教官");
                data.put("enemyCG", "m_000L.png");
                data.put("enemyType", "KI");
            } else {
                data.put("enemyName", mdKbrUserE.getPlayerName());
                data.put(
                    "enemyCG",
                    mdKbrUserE.getPlayerCGType().replace("R.png", "L.png"));
                data.put("enemyType", mdKbrUserE.getPlayerType());
            }
            if (mdUseKanjiE != null) {
                typeSpecialPointE =
                    serviceMdKanji.getTypeSpecialPoint(mdUseKanjiE);
                data.put("eneId", mdUseKanjiE.getUserId());
                data.put("enemyBarPoint", mdUseKanjiE.getUserBp().toString());
                data.put("enemyBarPointBefore", mdUseKanjiE
                    .getUserBp()
                    .toString());
                data
                    .put("enemyAttackPoint", mdUseKanjiE.getUserAp().toString());
                data.put("enemyKanji01", mdUseKanjiE.getUsingKanji01());
                data.put("enemyKanji02", mdUseKanjiE.getUsingKanji02());
                data.put("enemyKanji03", mdUseKanjiE.getUsingKanji03());
                data.put(
                    "enemyTypeSp1",
                    typeSpecialPointE.get("typeSpecialPoint1").toString());
                data.put(
                    "enemyTypeSp2",
                    typeSpecialPointE.get("typeSpecialPoint2").toString());
                data.put(
                    "enemyTypeSp3",
                    typeSpecialPointE.get("typeSpecialPoint3").toString());
                data.put(
                    "enemyTypeSp4",
                    typeSpecialPointE.get("typeSpecialPoint4").toString());
                data.put(
                    "enemyTypeSp5",
                    typeSpecialPointE.get("typeSpecialPoint5").toString());
            } else {
                data.put("eneId", "教官");
                data.put("enemyName", "教官");
                data.put("enemyBarPoint", enemyBarPoint.toString());
                data.put("enemyBarPointBefore", enemyBarPoint.toString());
                data.put("enemyAttackPoint", enemyAttackPoint.toString());
                data.put("enemyKanji01", "壱");
                data.put("enemyKanji02", "弐");
                data.put("enemyKanji03", "参");
                data.put("enemyTypeSp1", "0");
                data.put("enemyTypeSp2", "0");
                data.put("enemyTypeSp3", "0");
                data.put("enemyTypeSp4", "0");
                data.put("enemyTypeSp5", "0");
                data.put("enemyCG", "m_000L.png");
            }
        }
        data.put("enemyComment", "手加減しないぞ");
        data.put("attackStart", "");

        this.toJson(data);

        return null;

    }
}
